package org.example.system.controller.monitor;

import lombok.RequiredArgsConstructor;
import org.example.common.core.domain.R;
import org.example.common.core.utils.page.Pager;
import org.example.common.excel.utils.ExcelUtil;
import org.example.common.log.annotation.OperatorLog;
import org.example.common.log.enums.BusinessType;
import org.example.common.security.annotation.PreAuthorize;
import org.example.system.domain.vo.SysOperatorLogVo;
import org.example.system.service.ISysOperatorLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operlog")
public class SysOperlogController   {

    private final ISysOperatorLogService operLogService;

    /**
     * 获取操作日志记录列表
     */
    @PreAuthorize(hasAnyPermi = {"monitor:operlog:list"})
    @PostMapping("/list")
    public Pager<SysOperatorLogVo> list(@RequestBody  Pager pager) {
        return operLogService.selectPageOperLogList(pager);
    }

    /**
     * 导出操作日志记录列表
     */
    @OperatorLog(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasAnyPermi = {"monitor:operlog:export"})
    @PostMapping("/export")
    public void export(@RequestBody  Pager pager, HttpServletResponse response) {
        List<SysOperatorLogVo> list = operLogService.selectOperLogList(pager);
        ExcelUtil.exportExcel(list, "操作日志", SysOperatorLogVo.class, response);
    }

    /**
     * 批量删除操作日志记录
     * @param operIds 日志ids
     */
    @OperatorLog(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize(hasAnyPermi = {"monitor:operlog:remove"})
    @DeleteMapping("/{operIds}")
    public R<Void> remove(@PathVariable Long[] operIds) {
        return R.toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    /**
     * 清理操作日志记录
     */
    @PreAuthorize(hasAnyPermi = {"monitor:operlog:remove"})
    @OperatorLog(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R<Void> clean() {
        operLogService.cleanOperLog();
        return R.ok();
    }
}
